package com.cyh.greendaoupgrade.greendao;

import android.content.Context;

import com.cyh.greendaoupgrade.greendao.dao.BookItem2Dao;
import com.cyh.greendaoupgrade.greendao.dao.DaoSession;
import com.cyh.greendaoupgrade.greendao.dao.StudentItemDao;
import com.cyh.greendaoupgrade.greendao.dao.TeacherItemDao;
import com.cyh.greendaoupgrade.greendao.dao.UserItemDao;
import com.cyh.greendaoupgrade.greendao.model.BookItem2;
import com.cyh.greendaoupgrade.greendao.model.StudentItem;
import com.cyh.greendaoupgrade.greendao.model.TeacherItem;
import com.cyh.greendaoupgrade.greendao.model.UserItem;

import java.util.List;

public class DBUtils {
    private static final String TAG = "DBUtils";
    private static volatile DBUtils instance;
    private DaoSession mDaoSession;
    private GreenDaoHelper helper;
    private Context context;
    public DBUtils(Context context) {
        this.context = context;
    }

    public static DBUtils getInstance(Context context) {
        if (instance == null) {
            synchronized (DBUtils.class) {
                if (null == instance) {
                    // this.context = context.getApplicationContext();
                    instance = new DBUtils(context.getApplicationContext());
                }
            }
        }
        return instance;
    }

    public DaoSession getDaoSession() {
        if(null == mDaoSession){
            helper = new GreenDaoHelper(context);
            //mDaoSession = helper.initDao(PersistentCookieStore.getCookieStore().getStudentId());
            //数据库的名字在这里设置
            mDaoSession = helper.initDao("name");
        }
        return mDaoSession;
    }

    public void close() {
        if(helper != null) {
            helper.close();
        }
    }

    public void clear(){
        if (instance != null)
        {
            instance = null;
        }
        if (mDaoSession!=null)
        {
            mDaoSession = null;
        }

        if(helper != null) {
            helper = null;
        }
    }

    /********************************UserItemDao***********************************************/
    private UserItemDao getUserItemDao(){
        return getDaoSession().getUserItemDao();
    }

    public synchronized long insertUser(UserItem item){
        try {
            List<UserItem> list2 = getUserItemDao().queryBuilder()
                    .where(UserItemDao.Properties.Uuid.eq(item.getUuid()))
                    .build()
                    .list();
            getUserItemDao().deleteInTx(list2);

            return getUserItemDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }


    public void deleteAllUser(){
        getUserItemDao().deleteAll();
    }


    /********************************StudentItemDao***********************************************/
    private StudentItemDao getStudentItemDao(){
        return getDaoSession().getStudentItemDao();
    }

    public synchronized long insertStudent(StudentItem item){
        try {
            List<StudentItem> list2 = getStudentItemDao().queryBuilder()
                    .where(StudentItemDao.Properties.Uuid.eq(item.getUuid()))
                    .build()
                    .list();
            getStudentItemDao().deleteInTx(list2);

            return getStudentItemDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    /********************************TeacherItemDao***********************************************/
    private TeacherItemDao getTeacherItemDao(){
        return getDaoSession().getTeacherItemDao();
    }

    public synchronized long insertTeacher(TeacherItem item){
        try {
            return getTeacherItemDao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }

    /********************************BookItemDao***********************************************/
    private BookItem2Dao getBookItem2Dao(){
        return getDaoSession().getBookItem2Dao();
    }

    public synchronized long insertBook(BookItem2 item){
        try {
            return getBookItem2Dao().insert(item);
        } catch (Exception e) {
            e.printStackTrace();
        }
        return -1;
    }




}
